package com.chentf.rabbitmq.fanout02;

import com.chentf.rabbitmq.utils.RabbitMqUtilsSGG;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 分发广播-消费者2
 */
public class ReceiveLogs02 {

    //交换机名称
    public static final String EXCHANGE_NAME = "logs";

    public static void main(String[] args) throws IOException, TimeoutException {
        Channel channel = RabbitMqUtilsSGG.getChannel();
        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
        //声明一个队列，名称随机，当消费者断开与队列的连接时，队列自动删除
        String queueName = channel.queueDeclare().getQueue();
        //绑定交换机和队列
        //把该临时队列绑定我们的 exchange 其中 routingkey(也称之为 binding key)为空字符串
        channel.queueBind(queueName,EXCHANGE_NAME,"");
        System.out.println("等待接受消息，把接受到的消息打印在屏幕上...");

        DeliverCallback deliverCallback = (consumerTag,message) -> {
            System.out.println("ReceiveLogs02控制台打印接受到的消息：" + new String(message.getBody()));
        };
        channel.basicConsume(queueName,true,deliverCallback,consumerTag -> {});
    }
}
